﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using Aandelendelen.Obs;

namespace Aandelendelen.Controls
{
	public partial class MyPortfolioControl : AuthenticationRequiredBaseUserControl
	{
		bool export = false;

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.SetPortfolioDetails();
		}


		private void SetPortfolioDetails()
		{
			PortfolioDetails details = new PortfolioDetails(this.UserInfo.Id);

			this.GridView1.Columns[4].FooterText = ObjectFormatter.GetCurrencyString(details.TotalBuy);
			this.GridView1.Columns[7].FooterText = ObjectFormatter.GetCurrencyString(details.TotalValue);
			this.GridView1.Columns[8].FooterText = ObjectFormatter.GetPercentageString(details.TotalProfit);

			this.GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);

			this.GridView1.DataSource = details;

			this.GridView1.DataBind();
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			DataView dv = new DataView(new PortfolioDetails(this.UserInfo.Id));

			object sortCol = ViewState["portfoliosortcolumn"];
			object sortColDirection = ViewState["portfoliosortcolumndirection"];

			if (sortCol != null)
			{
				if (sortCol.Equals(e.SortExpression))
				{
					ViewState["portfoliosortcolumndirection"] = sortColDirection.Equals("ASC") ? "DESC" : "ASC";
				}
				else
				{
					ViewState["portfoliosortcolumndirection"] = "ASC";
				}
			}
			else
			{
				ViewState["portfoliosortcolumndirection"] = "ASC";
			}

			ViewState["portfoliosortcolumn"] = e.SortExpression;

			dv.Sort = e.SortExpression + " " + ViewState["portfoliosortcolumndirection"];

			this.GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
			this.GridView1.DataSource = dv;
			this.GridView1.DataBind();

		}

		void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				if (e.Row.Cells[8].Text.StartsWith("-"))
				{
					e.Row.Cells[8].CssClass = "negativeprofit";
				}
				else
				{
					e.Row.Cells[8].CssClass = "positiveprofit";
				}
			}
		}

		protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			this.GridView1.PageIndex = e.NewPageIndex;

			this.SetPortfolioDetails();
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if (!export)
				base.Render(writer);
			else
			{
				try
				{
					if (this.RadioButtonList1.SelectedValue == "Excel")
						this.RenderToExcel();
					else if (this.RadioButtonList1.SelectedValue == "Csv")
						this.RenderToCsv();
					else if (this.RadioButtonList1.SelectedValue == "Xml")
						this.RenderToXml();
					else
						base.Render(writer);
				}
				finally
				{
					this.export = false;
				}
			}
		}

		private void RenderToExcel()
		{
			string attachment = "attachment; filename=Portfolio.xls";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "application/vnd.ms-excel";

			StringWriter stw = new StringWriter();

			HtmlTextWriter htextw = new HtmlTextWriter(stw);

			bool allowPaging = this.GridView1.AllowPaging;

			this.GridView1.AllowPaging = false;
			this.SetPortfolioDetails();

			this.GridView1.RenderControl(htextw);

			this.GridView1.AllowPaging = allowPaging;
			this.SetPortfolioDetails();

			Response.Write(stw.ToString());

			Response.End();
		}

		private void RenderToCsv()
		{
			string attachment = "attachment; filename=AandelenDelen.txt";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "text/txt";

			StringWriter stw = new StringWriter();

			PortfolioDetails details = new PortfolioDetails(this.UserInfo.Id);

			details.SaveAsCSV(stw);

			Response.Write(stw.ToString());

			Response.End();
		}

		private void RenderToXml()
		{
			string attachment = "attachment; filename=AandelenDelen.xml";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "text/xml";

			StringWriter stw = new StringWriter();

			PortfolioDetails details = new PortfolioDetails(this.UserInfo.Id);

			details.WriteXml(stw);

			Response.Write(stw.ToString());

			Response.End();
		}

		protected void Button2_Click(object sender, EventArgs e)
		{
			this.export = true;
		}
	}

}